<template>
	<view class="order-confirm" v-if="user" :style="[{paddingTop:statusBar+ 'px'}]">
		<view class="status" :style="[{height:statusBar+ 'px'}]" ></view>
		<view class="navigation" :style="[{top:statusBar+ 'px'}]">
			<image src="/static/images/back.png" class="back" @click="back()" mode=""></image>
			<view class="navigation-title">确认订单</view>
		</view>
		<view class="navigation-position"></view>
		<order-address-entrance-com></order-address-entrance-com>
		<order-products-com :records="records"></order-products-com>
		<!-- <view class="price-info">
			<view class="price-info-title">价格明细</view>
			<view class="total-price">
				<view>商品总额</view>
				<view>￥{{ orderTotalAmount }}</view>
			</view>
		</view> -->
		
		<order-coupon-com  :user="user"  :isTake="isTake" :orderSource="orderSource" :totalAmount="totalAmount" @choiceCoupon="choiceCoupon"></order-coupon-com>
		 <payment-choose-com :user="user" @paymentChosen="choosePayment" :currentIndex="currentIndex"  ></payment-choose-com>
		
		<order-submit-button-com :needBrokerage="needBrokerage" :totalAmount ="realPrice" :records="records" :orderId="orderId" :uCouponId="uCouponId"></order-submit-button-com>
		<view class="content-item">
			<view class="content-item-left">用户购买须知</view>
			<image src="/static/images/rightmore.png" class="rightmore" mode=""></image>
		</view>
		<view class="content-item">
			<view class="content-item-left">用户发货须知</view>
			<image src="/static/images/rightmore.png" class="rightmore" mode=""></image>
		</view>
		
	</view>
</template>

<script>
	// /解决IOS返回页面不刷新的问题
		// var isPageHide = false;
		// window.addEventListener('pageshow', function() {
		// 		if(isPageHide) {
		// 				window.location.reload();
		// 		}
		// });
		// window.addEventListener('pagehide', function() {
		// 		isPageHide = true;
		// })
	import store from 'store'
	import {
		mapGetters,
		mapActions
	} from 'vuex'
	// import AuthChecking from "mixins/AuthChecking"
	import CurrentUser from "mixins/CurrentUser"
	import OrderAddressEntranceCom from "components/order/confirm/OrderAddressEntranceCom"
	import OrderProductsCom from "components/order/confirm/OrderProductsCom"
	import OrderCouponCom from "components/order/confirm/OrderCouponCom"
	import OrderSubmitButtonCom from "components/order/confirm/OrderSubmitButtonCom"
	import PaymentChooseCom from "components/common/PaymentChooseCom"
	export default {
		name: "confirm",
		store,
		mixins: [
			// AuthChecking,
			CurrentUser
		],
		components: {
			PaymentChooseCom,
			OrderAddressEntranceCom,
			OrderProductsCom,
			OrderCouponCom,
			OrderSubmitButtonCom
		},
		data() {
			return {
				needBrokerage:'true',
				isWeiXin:true,
				alipayShow:false,
				currentIndex:0,
				records:[],
				totalAmount:0,
				orderId:0,
				isTake:false,
				orderSource:'common',
				productToPrice:[],
				dPrice:0,
				uCouponId:0,
				realPrice:0,
			}
		},
		computed: {
			...mapGetters([
				'product',
				'selectedSku',
				'selectedSkuCount',
				'orderTotalAmount',
				'statusBar'
			])
		},
		methods: {
			...mapActions([
				'updateVipPriceSavingMoney',
				'updateOrderTotalAmount',
				'updateOrderPayment'
			]),
			back(){
				uni.navigateBack()
			},
			async refreshSavingMoney() {
				const normalPrice = this.selectedSku.price
				const vipPrice = this.selectedSku.price_vip
				const normalMoney = normalPrice * this.selectedSkuCount
				const vipMoney = vipPrice * this.selectedSkuCount
				await this.updateVipPriceSavingMoney(normalMoney - vipMoney)
			},
			async getOrderTotalAmount() {
				let price = this.selectedSku.price_vip
				// if (this.user.grade !== 'normal' ) {
				// 	price = this.selectedSku.price_vip
				// }
				// price = this.selectedSku.price_vip
				let totalAmount = price * this.selectedSkuCount
				return totalAmount.toFixed(2)
			},
			async choosePayment(payment,index) {
				this.currentIndex = index
				await this.updateOrderPayment(payment)
			},
			choiceCoupon(params) { //选项卡切换
				this.dPrice = params.decrementPrice
				this.uCouponId = params.userCouponId
				this.realPrice = this.totalAmount - this.dPrice;
				console.log(this.uCouponId);
			},
			checkboxChange: function(e) {},
			isWeiXinLogin() {
				var ua = window.navigator.userAgent.toLowerCase();
				if (ua.match(/MicroMessenger/i) == 'micromessenger') {
					return true;
				} else {
					return false;
				}
			}
		},
		async onShow() {
			await this.getCurrentUser()
			/* await this.refreshSavingMoney()
			await this.updateOrderTotalAmount(await this.getOrderTotalAmount()) */
			if(this.isWeiXinLogin()){
				this.isWeiXin = true;
				this.alipayShow = false;
			}else{
				this.isWeiXin = false
				this.alipayShow = true
			}
		},
		async onLoad(options) {
			this.records = JSON.parse(decodeURIComponent(options.records))
			if(options.order_id){
				this.orderId = options.order_id;
				//console.log(this.orderId);
			}
			
			/* let productToPrice = []; */
			if(this.records.length>0){
				if(this.records[0].attr){
					
					this.totalAmount = this.records[0].price;
					this.isTake = true;
				}else{
					this.records.forEach((record, index) => {
						this.totalAmount += record.sku.price_vip * record.number;
						
						/* if(productToPrice[record.product.id]){
							
							productToPrice[record.product.id].price += record.sku.price_vip * record.number;
						}else{
							productToPrice[record.product.id] = {'id':record.product.id,'price': record.sku.price_vip * record.number};
						} */
					})
					
				}
				
			}
			this.realPrice  = this.totalAmount;
			
			/* if(productToPrice){
				this.productToPrice = productToPrice.filter(function (s) {
					return s;
				});
				
				this.productToPrice.sort(function(a, b){
					
					return a.price - b.price
					
				})
			}
			 */
			
			
			
			
			if(this.isWeiXinLogin()){
				this.isWeiXin = true;
				this.alipayShow = false
			}else{
				this.isWeiXin = false
				this.alipayShow = true
			}

			/* if(this.product.is_gold_bean){
				this.currentIndex = 3
				await this.updateOrderPayment('goldcoinsmoney')
			} */
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background: #F6F6F6;
	}
	.status {
		width: 100%;
		position: fixed;
		top: 0;
		z-index: 99;
		background: #F6F6F6;
	}
	.navigation{
		width: 100%;
		height: 100rpx;
		background: #F6F6F6;
		display: flex;
		align-items: center;
		justify-content: center;
		position: fixed;
		left:0;
		top:0;
		z-index:999;
	}
	.navigation-position{
		width: 100%;
		height: 100rpx;
		background: rgba(0,0,0,0);
	}
	.back{
		display: block;
		width: 48rpx;
		height: 48rpx;
		position: absolute;
		top: 26rpx;
		left:22rpx;
	}
	.navigation-title{
		font-size: 32rpx;
		font-weight: 500;
		color: #333333;
	}
	.order-confirm {
		padding: 20rpx;
		// min-height: 100vh;
		background: #F6F6F6;
		padding-bottom: 250rpx;
	}

	.price-info {
		// width: 710rpx;
		width: 100%;
		height: 160rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
	}

	.price-info-title {
		font-size: 32rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #333330;
		margin-left: 24rpx;
		padding-top: 24rpx;
	}

	.total-price {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 24rpx;
		margin-top: 26rpx;

		>view:nth-child(1) {
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #333330;
		}

		>view:nth-child(2) {
			font-size: 32rpx;
			font-family: PingFangSC-Semibold, PingFang SC;
			font-weight: 600;
			color: #333330;
		}
	}
	.checkbox-group{
		display: flex;
		align-items: center;
		width: 100%;
		height: 100%;
		background: linear-gradient(90deg, rgba(253, 245, 235, 0) 0%, #FDF5EB 100%);
		border-radius: 12rpx;
	}
	.lable{
		display: flex;
		align-items: center;
		width: 100%;
		height: 100%;
		border-radius: 12rpx;
	}
	
	.open-vip-card-content{
		width: 100%;
		height: 100%;
		background: linear-gradient(90deg, rgba(253, 245, 235, 0) 0%, #FDF5EB 100%);
		border-radius: 12rpx;
	}

	.open-vip-card-box {
		width: 706rpx;
		height: 202rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
		margin-top: 10rpx;
		padding: 2rpx;
	}

	.open-vip-card-title {
		width: 116rpx;
		height: 36rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background: #F5DBBF;
		border-radius: 16rpx 0px 16rpx 0px;
		font-size: 20rpx;
		font-family: PingFangSC-Semibold, PingFang SC;
		font-weight: 600;
		color: #613B23;
	}
	
	

	.open-vip-card-info {
		display: flex;
		align-items: center;

		>view:nth-child(1) {
			margin: 16rpx 0 0 70rpx;

			>view:nth-child(1) {
				font-size: 28rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				line-height: 40rpx;
			}

			>view:nth-child(2) {
				font-size: 32rpx;
				font-family: PingFangSC-Semibold, PingFang SC;
				font-weight: 600;
				color: #333333;
				line-height: 44rpx;
			}

			>view:nth-child(3) {
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
				line-height: 34rpx;
			}
		}

		>view:nth-child(2) {
			display: flex;
			align-items: baseline;
			margin: 0 26rpx 0 60rpx;

			>text:nth-child(1) {
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #FF4924;
			}

			>text:nth-child(2) {
				font-size: 32rpx;
				font-family: PingFangSC-Semibold, PingFang SC;
				font-weight: 600;
				color: #FF4924;
			}
		}
	}
	.checked-box{
		margin-top:10rpx;
	}
	
	.content-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		width:718rp;
		height:106rpx;
		background:#fff;
		border-radius: 10rpx;
		margin-top:20rpx;
	}
	
	.rightmore {
		display: block;
		width: 32rpx;
		height: 32rpx;
		margin-right:26rpx;
	}
	
	.content-item-left {
		display: flex;
		align-items: center;
		margin-left:28rpx;
		font-size:28rpx;
	}
	
</style>
